/** \brief File implementing a class storing the rules.
 *
 * This file contains the implementation of a class which stores the game
 * rules.
 *
 * @file RuleData.cpp
 * @author Johan "Solo" Luisier
 * @date 2013/04/24
 */
#include "RuleData.hpp"

#include <algorithm>

using namespace std;

RuleData::RuleData( const quint8& maxNeighbours )
    : MaxNeighbours( maxNeighbours )
    , BirthConditions()
    , SurvivalConditions()
{}

RuleData::~RuleData()
{
    BirthConditions.clear();
    SurvivalConditions.clear();
}

bool RuleData::addBirthCondition( const quint8& bCond )
{
    if ( BirthConditions.contains( bCond ) )
        return false;

    if ( bCond > MaxNeighbours )
        return false;

    BirthConditions.append( bCond );

    sort( BirthConditions.begin(), BirthConditions.end() );

    return true;
}

bool RuleData::removeBirthCondition( const quint8& bCond )
{
    int loc( BirthConditions.indexOf( bCond ) );
    if ( loc == -1 )
        return false;

    BirthConditions.removeAt( loc );

    return true;
}

bool RuleData::addSurvivalCondition( const quint8& sCond )
{
    if ( SurvivalConditions.contains( sCond ) )
        return false;

    if ( sCond > MaxNeighbours )
        return false;

    SurvivalConditions.append( sCond );

    sort( SurvivalConditions.begin(), SurvivalConditions.end() );

    return true;
}

bool RuleData::removeSurvivalCondition( const quint8& sCond )
{
    int loc( SurvivalConditions.indexOf( sCond ) );
    if ( loc == -1 )
        return false;

    SurvivalConditions.removeAt( loc );

    return true;
}
